mod logic;
mod parts;

use crate::parts::*;
use anyhow::Result;
use clap::Parser;
use colored::*;
use std::time::Instant;

fn main() -> Result<()> {
    process()
}

fn process() -> Result<()> {
    // --- 1. 获取输入参数 ---
    let args = Args::parse();
    let start_time = Instant::now();

    // --- 2. 打印执行内容 ---
    println!(
        "[ RU-CLEANSER v1.1.1 by Lys]\n
        {} Starting scan in '{}'...",
        "INFO".green(),
        args.path.display()
    );

    // --- 3. 执行扫描 ---
    let targets = logic::scan_path(&args.path)?;

    // --- 4. 扫描结果空，终止流程 ---
    if targets.is_empty() {
        println!(
            "\n{}",
            "No project build artifacts found. Your disk is clean!".green()
        );
        return Ok(());
    }

    // --- 5. 打印报告 ---
    logic::display_report(&targets);

    // --- 6. 确认和删除 ---
    match args.delete {
        true => match logic::confirm_deletion() {
            Ok(b) => match b {
                true => logic::delete_targets(&targets),
                _ => println!("{}", "Deletion cancelled by user.".yellow()),
            },
            Err(_) => {}
        },
        false => {
            println!("\nSkipping deleting. Run with the --delete flag to remove the directories.");
        }
    }

    // --- 7. 打印最终总结 ---
    println!(
        "\n✨ Rust-Cleanser scan and clean , finished in {:.2?}.",
        start_time.elapsed(),
    );

    Ok(())
}
